package com.maple.admin.misc.cookie.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.Cookie;

/**
 * cookie operations wrapper
 * @author luobenyu
 *
 */
public class CookieHelper {
	
	private static CookieHelper instance;
	
	private CookieHelper(){
		
	}
	
	public static CookieHelper getInstance(){
		if(instance == null){
			instance = new CookieHelper();
		}
		return instance;
	}
	
	/**
	 * create a single cookie
	 * @param key
	 * @param val
	 * @return
	 */
	public Cookie createCookie(String key, String val){
		return new Cookie(key,val);
	}
	
	/**
	 * create a list of cookies
	 * @param cookieMap key-value match
	 * @return
	 */
	public List<Cookie> createCookies(Map<String,Object> cookieMap){
		List<Cookie> cookies = new ArrayList<Cookie>();
		Iterator<Entry<String, Object>> entries = cookieMap.entrySet().iterator();
		while(entries.hasNext()){
		    Entry<String, Object> entry = entries.next();
		    String key = entry.getKey();
		    String value = entry.getValue().toString();
		    Cookie c = new Cookie(key,value);
		    cookies.add(c);
		}
		return cookies;
		
	}
	
}
